import DialogStore from ".";
import { observable, action, toJS } from "mobx";
import { Button, message } from 'antd';
import { each } from 'lodash';
import { WeaLocaleProvider } from "ecCom";
import API from '../../apis/cuspath';
const getLabel = WeaLocaleProvider.getLabel;

export default class JoinCubeDialogStore extends DialogStore {

    @observable nodeid;
    @observable pathStore;
    @observable readonly = false;
    @observable joinSetting = [];
    @observable syncType = 0;
    @observable syncNodeId;
    @observable pathid;



    constructor(pathStore) {
        super();
        this.title = getLabel('510979','关联建模模块');
        this.style = { width: 720, height: 400 };
        this.pathStore = pathStore;
        this.hasScroll = true;
        message.config({ top: 60 });
    }


    @action open = (params) => {
        this.params = {};
        each(params, (value, key) => {
            if (value) {
                this.params[key] = value;
            }
        });
        this.readonly = this.pathStore.readonly
        this.buttons = this.getButtons();
        this.nodeid = this.params.nodeid;
        this.pathid = this.params.pathid;
        this.syncType = 0;
        this.visible = true;
        //this.loading = true;
        API.getJoinCubeSetting(this.params).then(result => {
            //this.loading = false;
            this.joinSetting = result;
        });
    }

    @action save = () => {
        if (this.checkParams()) {
           const joinSetting = this.getParams();
           API.saveJoinCubeSetting({nodeid:this.nodeid,joinSetting:JSON.stringify(joinSetting),syncType:this.syncType,syncNodeId:this.syncNodeId}).then(()=>{
                message.success(getLabel('502231','保存成功'));
                this.hide();
                this.joinSetting = [];
           })
        }
    }

    checkParams = () => {
        let flag = true;
        this.joinSetting.map(setting => {
            if (setting.options.length > 0) {
                if (setting.isUsed == 1 && setting.formmodeId == null) {
                    this[setting.formId].showError();
                    flag = false;
                }
            }
        })
        return flag;
    }

    getParams = () => {
        const arr = [];
        this.joinSetting.map(setting => {
            if(setting.options.length>0 && setting.isUsed==1) arr.push({ 'formId': setting.formId, 'isUsed': setting.isUsed, 'formmodeId': setting.formmodeId })
        })
        if(arr.length>0) {
            this.pathStore.nodeForm.joinCubeFlag = true;
        }else {
            this.pathStore.nodeForm.joinCubeFlag = false;
        }
        return arr;
    }


    @action getButtons = () => {
        const buttons = [<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@i5tqno@cancel`} key="cancel" type="primary" onClick={this.hide}>{getLabel(31129, '取消')}</Button>]
        if (!this.readonly) buttons.push(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@ld1e8r@save`} key="save" type="primary" onClick={this.save}>{getLabel(30986, '保存')}</Button>);
        return buttons;
    }



    @action changeSetting = (key, value, formId) => {
        this.joinSetting.map(setObj => {
            if (formId === setObj.formId) {
                setObj[key] = value;                   
                if(key ==='isUsed' && value ==='0') setObj.formmodeId = null;
            }
        })
        this.joinSetting = [...this.joinSetting]
    }

}
